#include "headers/Tracker.h"
#include "headers/Config.h"

#include <time.h>
#include <stdio.h>
#include <string.h>

Tracker trac_data[TAMANHO_TRACKER];
unsigned long date_count = 0;

int getIndexPagina(int gid);

void CriarTracker(){
	int i;
	for (i = 0; i < TAMANHO_TRACKER; ++i) {
		trac_data[i].gid = -1;
		trac_data[i].flag = NULL;
		trac_data[i].offset = -1;
		trac_data[i].hits = 0;
		trac_data[i].alteracoes = 0;
		trac_data[i].date_entrada = -1;
		trac_data[i].date = -1;
		trac_data[i].dirty = 0;
	}

}


void trac_AtualizarPagina(int gid, char flag, int offset){
	trac_data[gid].gid = gid;
	trac_data[gid].flag = flag;
	trac_data[gid].offset = offset;
	trac_data[gid].dirty = 0;

	if(trac_data[gid].date_entrada == -1)
		trac_data[gid].date_entrada = date_count;

}

void trac_AlterarPagina(int gid, char flag, int offset){
	int pos = getIndexPagina(gid); //Checar se realmente � necess�rio esta fun��o
	trac_data[pos].flag = flag;
	trac_data[pos].offset = offset;
	trac_data[pos].hits++;
    trac_data[gid].date = date_count;
    trac_data[gid].dirty = 0;
    date_count++;

	/*int boolFind = 0;
	int i = 0;
	 while(boolFind != 1){
		if(trac_data[i].gid == gid){
			boolFind = 1;

			trac_data[i].flag = flag;
			trac_data[i].offset = offset;
		}
		i++;
	}*/

	/*trac_data[gid].flag = flag;
	trac_data[gid].offset = offset;*/ //Caso o gid seja criado incrementalmente e sua posi��o na track seja relativa ao gid,
									  //ou seja, n�o precisa de um la�o buscando a pagina, o acesso � direto.

}

void trac_HitDate(int gid){
	trac_data[gid].hits++;
	trac_data[gid].date = date_count;
	date_count++;
}

void trac_HitDateAlter(int gid){
	trac_data[gid].alteracoes++;
	trac_data[gid].hits++;
	trac_data[gid].date = date_count;
	//trac_data[gid].dirty = 0;
	date_count++;
}

void trac_LocalizarPagina(int gid, char* flag, int* offset){

	int index = getIndexPagina(gid);

	if(index != -1){

	memcpy(flag, &trac_data[index].flag, sizeof(char)); //sizeof(char) antes era 1
	memcpy(offset, &trac_data[index].offset, sizeof(int)); //sizeof(int) antes era 4

	}else{
		flag = NULL;
		offset = NULL;
	}


}

void trac_ResetarPagina(int gid){
	trac_data[gid].flag = NULL;
	trac_data[gid].offset = -1;
	trac_data[gid].hits = 0;
	trac_data[gid].alteracoes = 0;
	trac_data[gid].date_entrada = -1;
	trac_data[gid].date = -1;
	trac_data[gid].gid = -1;
	trac_data[gid].date = date_count;
	trac_data[gid].dirty = 0;
}

int getIndexPagina(int gid){
	int boolFind = 0;
	int i = 0;
	int pos = -1;

	while(boolFind != 1 && i < TAMANHO_TRACKER){
		if(trac_data[i].gid == gid){
				boolFind = 1;
				pos = i;
		}
		i++;
	}
	return pos;
}


void MostrarTracker(){
	int i;
	for (i = 0; i < TAMANHO_TRACKER; i++) {

		if(trac_data[i].gid != -1){

		printf("Posicao no Tracker: %d\n", i);

		printf("GID: %d\n", trac_data[i].gid);
		printf("flag: %c\n", trac_data[i].flag);
		printf("Offset: %d\n", trac_data[i].offset);
		printf("Ultima Alteracao: %lu\n", trac_data[i].date);
		printf("------------------------------------------\n");
		}
	}
}


/*int LRU(){
	time_t dateAux = time(0);
	char localAux;
	int offsetLRU = -1;

	int i;
	for (i = 0; i < TAMANHO_TRACKER; ++i) {
		localAux = trac_data[i].flag;

		if(dateAux > trac_data[i].date && localAux == 'B'){
			dateAux = trac_data[i].date;
			offsetLRU = trac_data[i].offset;
		}

	}

	return offsetLRU;
}*/

void ApagarTracker(){


}

